package com.java.miaosha.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.miaosha.dao.OrderDao;
import com.java.miaosha.entity.MiaoshaOrderEntity;
import com.java.miaosha.entity.OrderEntity;
import com.java.miaosha.entity.UserEntity;
import com.java.miaosha.service.MiaoshaOrderService;
import com.java.miaosha.service.MiaoshaService;
import com.java.miaosha.service.OrderService;
import com.java.miaosha.util.IdWorker;
import com.java.miaosha.vo.GoodsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;


@Service("orderService")
public class OrderServiceImpl extends ServiceImpl<OrderDao, OrderEntity> implements OrderService {
	@Autowired
	private OrderDao orderDao;
	@Autowired
	private MiaoshaOrderService miaoshaOrderService;

	@Override
	@Transactional
	public OrderEntity createOreder(UserEntity user, GoodsVO goods) {
		OrderEntity order = new OrderEntity();
		order.setId(String.valueOf(new IdWorker().nextId()));
		order.setCreateDate(new Date());
		order.setDeliveryAddrId(1L);
		order.setGoodsCount(1);
		order.setGoodsId(goods.getId());
		order.setGoodsName(goods.getGoodsName());
		order.setGoodsPrice(goods.getMiaoshaPrice());
		order.setOrderChannel(1);
		order.setStatus(0);
		order.setUserId(user.getId());
		orderDao.insert(order);

		MiaoshaOrderEntity miaoshaOrder = new MiaoshaOrderEntity();
		miaoshaOrder.setOrderId(order.getId());
		miaoshaOrder.setGoddsId(goods.getId());
		miaoshaOrder.setUserId(user.getId());
		miaoshaOrderService.save(miaoshaOrder);
		return order;
	}
}